#include<cstdio>
#include<cstring>
int T,n=0,x,y,z,r,cd,a[10005][6],t[6],v1,v2;char c[1005],s[1005];
int main(){
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	scanf("%d",&T);
	for(int k=1;k<=T;k++){
		scanf("%s",s+1);x=0;y=0;z=0;r=1;v1=0;v2=0;
		scanf("%s",c+1);cd=strlen(c+1);
//		printf("%s %s\n",s+1,c+1);
		for(int i=1;i<=cd;i++){
			if(c[i]>='0'&&c[i]<='9'){
				if(y==1&&x==0)z=1;
				if(y==0)y=1;
				x=x*10+c[i]-'0';
			}
			if(c[i]=='.'){
				if(r==4||y==0)z=1;
				if(x>255)z=1;
				t[r]=x;r++;
				x=0;y=0;v1++;
			}
			if(c[i]==':'){
				if(r!=4||y==0)z=1;
				if(x>255)z=1;
				t[r]=x;r++;
				x=0;y=0;v2++;
			}
		}
		if(v1>3||v2>1)z=1;
		if(x>65535)z=1;
		t[5]=x;
//		printf("%d %d %d %d %d %d\n",t[1],t[2],t[3],t[4],t[5],z);
		if(z==1)printf("ERR\n");
		else{
			y=0;
			if(s[1]=='S'){
				for(int i=1;i<=n;i++){
					if(a[i][1]==t[1]&&a[i][2]==t[2]&&a[i][3]==t[3]&&a[i][4]==t[4]&&a[i][5]==t[5]){y=1;break;}
				}
				if(y==1)printf("FAIL\n");
				else{n++;a[n][0]=k;a[n][1]=t[1];a[n][2]=t[2];a[n][3]=t[3];a[n][4]=t[4];a[n][5]=t[5];printf("OK\n");}
			}
			else{
				for(int i=1;i<=n;i++){
					if(a[i][1]==t[1]&&a[i][2]==t[2]&&a[i][3]==t[3]&&a[i][4]==t[4]&&a[i][5]==t[5]){y=a[i][0];break;}
				}
				if(y!=0)printf("%d\n",y);
				else printf("FAIL\n");
			}
		}
	}
}
